package com.laf.manage.controller.admin;

import com.laf.common.annotation.Log;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.page.TableDataInfo;
import com.laf.common.core.text.Convert;
import com.laf.common.enums.BusinessType;
import com.laf.common.utils.ServletUtils;
import com.laf.common.utils.poi.ExcelUtil;
import com.laf.manage.domain.po.ReceiveRecord;
import com.laf.manage.domain.vo.ReceiveRecordVO;
import com.laf.manage.service.IReceiveRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import static com.laf.common.core.page.TableSupport.PAGE_NUM;
import static com.laf.common.core.page.TableSupport.PAGE_SIZE;

/**
 * 领取记录Controller
 *
 * @author zian
 * @date 2024-11-01
 */
@Slf4j
@Api(tags = "领取记录管理")
@RestController("adminReceiveRecordController")
@RequestMapping("/admin/receiveRecord")
public class ReceiveRecordController extends BaseController {
    @Autowired
    private IReceiveRecordService receiveRecordService;


    @ApiOperation("查询领取记录列表")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReceiveRecord receiveRecord) {
        log.info("查询失物管理列表 参数有：{}", receiveRecord);
        Integer pageNum = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        TableDataInfo vo = receiveRecordService.selectReceiveRecordList(receiveRecord, pageNum, pageSize);
        return vo;
    }

    /**
     * 导出领取记录列表
     */
    @ApiOperation("导出领取记录列表")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:export')")
    @Log(title = "领取记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReceiveRecord receiveRecord) {
        List<ReceiveRecord> list = receiveRecordService.selectReceiveRecordList(receiveRecord);
        ExcelUtil<ReceiveRecord> util = new ExcelUtil<ReceiveRecord>(ReceiveRecord.class);
        util.exportExcel(response, list, "领取记录数据");
    }

    /**
     * 获取领取记录详细信息
     */
    @ApiOperation("获取领取记录详细信息")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ReceiveRecordVO vo = receiveRecordService.getInfoById(id);
        return success(vo);
    }


    /**
     * 根据物品id查询领取记录详情
     */
    @ApiOperation("获取领取记录详细信息")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:query')")
    @GetMapping(value = "/ItemId/{itemId}")
    public AjaxResult getInfoByItemId(@PathVariable("itemId") Long itemId) {
        ReceiveRecordVO vo = receiveRecordService.getInfoByItemId(itemId);
        return success(vo);
    }

    /**
     * 新增领取记录
     */
    @ApiOperation("新增领取记录")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:add')")
    @Log(title = "领取记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReceiveRecord receiveRecord) {
        return toAjax(receiveRecordService.save(receiveRecord));
    }

    /**
     * 修改领取记录
     */
    @ApiOperation("修改领取记录")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:edit')")
    @Log(title = "领取记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReceiveRecord receiveRecord) {
        return toAjax(receiveRecordService.updateById(receiveRecord));
    }

    /**
     * 删除领取记录
     */
    @ApiOperation("删除领取记录")
    @PreAuthorize("@ss.hasPermi('manage:receiveRecord:remove')")
    @Log(title = "领取记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(receiveRecordService.removeByIds(Arrays.asList(ids)));
    }
}
